package com.design.pattern.proxy;

import com.design.pattern.proxy.dynamic.cglib.CglibMethodInterceptor;
import com.design.pattern.proxy.dynamic.jdk.Subject;
import com.design.pattern.proxy.dynamic.jdk.SubjectInvocationHandler;
import com.design.pattern.proxy.statical.Proxy;
import com.design.pattern.proxy.statical.RealSubject;

/**
 * @author : 翌擎科技
 * @date : 2022/9/20 21:16
 */
public class TestDemo {

    public static void main(String[] args) {
//        jdkProxy();
        cglibProxy();
    }

    public static void staticilProxy(){
        //静态代理
        Proxy proxy=new Proxy(new RealSubject());
        proxy.request();
    }

    public static void jdkProxy(){
        Subject subject=new com.design.pattern.proxy.dynamic.jdk.RealSubject();
        SubjectInvocationHandler handler = new SubjectInvocationHandler(subject);
        Subject instance = handler.getInstance(subject.getClass());
        instance.request();

    }

    public static void cglibProxy(){
        Subject subject=new com.design.pattern.proxy.dynamic.jdk.RealSubject();
        Subject instance = new CglibMethodInterceptor().getInstance(subject.getClass());
        //注意 final修饰的方法，无法被代理
        instance.request();
    }

}
